package com.ymatou.iapi.productmanager.testcase;

import com.ymatou.iapi.productmanager.parameter.QueryCatalogCommissionsBean;
import com.ymatou.iapi.productmanager.parameter.SaleCommissionKvDto;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productmanager.service.QueryCatalogCommissionsCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.EnvSetup;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
*批量查询商品销售佣金
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_QueryCatalogCommissions{
	private static QueryCatalogCommissionsBean querycatalogcommissionsBean;
	private static QueryCatalogCommissionsCall querycatalogcommissionsCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("globalSellerId"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量查询商品销售佣金");
	}

	@Before
	public void caseUp() {
		querycatalogcommissionsBean=new QueryCatalogCommissionsBean();
		querycatalogcommissionsCall=new QueryCatalogCommissionsCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(querycatalogcommissionsCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryCatalogCommissions_001() {
		Logger.start(true,"批量查询商品销售佣金,单规格");
		try {
			AddSellerProductRequest addSellerProductRequest = ProductManagerCallServiceV2.initAddSellerGlobalPspProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addSellerProductRequest).get("Data").getAsJsonObject().get("ProductId").getAsString();
			
			//手动调同步mongo
			ProductManagerCallServiceV3.createProduct_sync(productId);
			
			String catalogId = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).get(0).getsCatalogId();
			BigDecimal saleCommission = new BigDecimal("0.9");
			ProductManagerCallServiceV2.UpdateCatalogCommisions(addSellerProductRequest.getSellerId(),productId,catalogId,addSellerProductRequest.getSellerName(),saleCommission);
			querycatalogcommissionsBean.setCatalogIds(Arrays.asList(catalogId));
			querycatalogcommissionsCall.setData(querycatalogcommissionsBean);
			querycatalogcommissionsCall.callService();
			Logger.verifyEquals("200",querycatalogcommissionsCall.getString("Code"),"验证Code");
            querycatalogcommissionsCall.getCommissionList().forEach(p->{
                Logger.verifyEquals(catalogId,p.getCatalogId(),"验证CatalogId");
                Logger.verifyEquals(saleCommission.intValue(),p.getSaleCommission().intValue(),"验证saleCommission");
                Logger.verifyEquals(0,p.getGiftType(),"验证GiftType");
                Logger.verifyEquals(null,p.getSaleCommissionList(),"验证GiftType");
            });
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryCatalogCommissions_002() {
		Logger.start(true,"批量查询商品销售佣金,多规格");
		try {
			AddSellerProductRequest addSellerProductRequest = ProductManagerCallServiceV2.initAddSellerGlobalPspProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addSellerProductRequest).get("Data").getAsJsonObject().get("ProductId").getAsString();
			
			//手动调同步mongo
			ProductManagerCallServiceV3.createProduct_sync(productId);
			
			String catalogId1 = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).get(0).getsCatalogId();
			String catalogId2 = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).get(1).getsCatalogId();
			BigDecimal saleCommission1 = new BigDecimal("0.800");
			BigDecimal saleCommission2 = new BigDecimal("0.800");
			ProductManagerCallServiceV2.UpdateCatalogCommisions(addSellerProductRequest.getSellerId(),productId,catalogId1,addSellerProductRequest.getSellerName(),saleCommission1);
			ProductManagerCallServiceV2.UpdateCatalogCommisions(addSellerProductRequest.getSellerId(),productId,catalogId2,addSellerProductRequest.getSellerName(),saleCommission2);
			querycatalogcommissionsBean.setCatalogIds(Arrays.asList(catalogId2,catalogId1));
			querycatalogcommissionsCall.setData(querycatalogcommissionsBean);
			querycatalogcommissionsCall.callService();
			Logger.verifyEquals("200",querycatalogcommissionsCall.getString("Code"),"验证Code");
			List<SaleCommissionKvDto> saleCommissionKvDtos = querycatalogcommissionsCall.getCommissionList();
			saleCommissionKvDtos.forEach(p->{
				Logger.verifyEquals(0,p.getGiftType(),"验证GiftType");
				if(p.getCatalogId().equals(catalogId1)){
					Logger.verifyEquals(saleCommission1.intValue(),p.getSaleCommission().intValue(),"验证saleCommission1");
				}else{
					Logger.verifyEquals(saleCommission2.intValue(),p.getSaleCommission().intValue(),"验证saleCommission2");
				}
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryCatalogCommissions_003() {
		Logger.start(true,"批量查询商品销售佣金,规格为空");
		try {
			querycatalogcommissionsBean.setCatalogIds(null);
			querycatalogcommissionsCall.setData(querycatalogcommissionsBean);
			querycatalogcommissionsCall.callService();
			Logger.verifyEquals("400",querycatalogcommissionsCall.getString("Code"),"验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryCatalogCommissions_004() {
		Logger.start(true,"批量查询商品销售佣金,规格不存在");
		try {
			querycatalogcommissionsBean.setCatalogIds(Arrays.asList("woqdhi"));
			querycatalogcommissionsCall.setData(querycatalogcommissionsBean);
			querycatalogcommissionsCall.callService();
			Logger.verifyEquals("201",querycatalogcommissionsCall.getString("Code"),"验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	
	/////////////////////////////////// 忽略case///////////////////////////////////////
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_QueryCatalogCommissions_005() {
		Logger.start(true,"查询优选团商品销售佣金 单个(全球优选业务下架)");
		try {
			//创建一个商品，获取商品id，设置为优选团商品
			AddSellerProductRequest addSellerProductRequest = ProductManagerCallServiceV2.initAddSellerGlobalPspProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addSellerProductRequest).get("Data").getAsJsonObject().get("ProductId").getAsString();
			ProductManagerCallServiceV2.AddSellerGlobalPspProduct(sellerId,productId,2);
			//设置佣金比例、获取规格id、根据规格id，商品id，修改商品佣金
			BigDecimal saleCommission = new BigDecimal("0.0800");
			List<String> catalogIds = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(x->x.getsCatalogId()).collect(Collectors.toList());
			ProductManagerCallServiceV2.UpdateCatalogCommisions(addSellerProductRequest.getSellerId(),productId,catalogIds.get(0),addSellerProductRequest.getSellerName(),saleCommission);
			ProductManagerCallServiceV2.UpdateCatalogCommisions(addSellerProductRequest.getSellerId(),productId,catalogIds.get(1),addSellerProductRequest.getSellerName(),saleCommission);
			//查询商品佣金，验证code
			querycatalogcommissionsBean.setCatalogIds(catalogIds);
			querycatalogcommissionsCall.setData(querycatalogcommissionsBean);
			querycatalogcommissionsCall.callService();
			Logger.verifyEquals("200",querycatalogcommissionsCall.getString("Code"),"验证Code");
			querycatalogcommissionsCall.getCommissionList().forEach(p->{
				if (p.getCatalogId().equals(catalogIds.get(0))){
					System.out.println(p);
//					Logger.verifyEquals(2,p.getGiftType(),"验证GiftType");
					Logger.verifyEquals(new BigDecimal("1.280000"),p.getSaleCommissionMapNew().get(0),"验证优选团佣金");
				}
			});

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_QueryCatalogCommissions_006() {
		Logger.start(true,"批量查询礼包商品销售佣金(全球优选业务下架)");
		try {
			AddSellerProductRequest addSellerProductRequest = ProductManagerCallServiceV2.initAddSellerGlobalPspProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addSellerProductRequest).get("Data").getAsJsonObject().get("ProductId").getAsString();
			ProductManagerCallServiceV2.AddSellerGlobalPspProduct(sellerId,productId,1);

			List<String> catalogIds = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(x->x.getsCatalogId()).collect(Collectors.toList());
			ProductManagerCallServiceV2.UpdateCatalogCommisionsV2(sellerId,productId,catalogIds.get(0),addSellerProductRequest.getSellerName(), Arrays.asList(new BigDecimal("0.0700"),new BigDecimal("0.0800"),new BigDecimal("0.0900"),new BigDecimal("0.1000")));
			ProductManagerCallServiceV2.UpdateCatalogCommisionsV2(sellerId,productId,catalogIds.get(1),addSellerProductRequest.getSellerName(), Arrays.asList(new BigDecimal("0.0700"),new BigDecimal("0.0800"),new BigDecimal("0.0900"),new BigDecimal("0.1000")));

			querycatalogcommissionsBean.setCatalogIds(catalogIds);
			querycatalogcommissionsCall.setData(querycatalogcommissionsBean);
			querycatalogcommissionsCall.callService();
			Logger.verifyEquals("200",querycatalogcommissionsCall.getString("Code"),"验证Code");
			querycatalogcommissionsCall.getCommissionList().forEach(p->{
				if (p.getCatalogId().equals(catalogIds.get(0))){
					System.out.println(p);
					Logger.verifyEquals(1,p.getGiftType(),"验证GiftType");
					Logger.verifyEquals(new BigDecimal("1.120000"),p.getSaleCommissionMapNew().get(1),"验证1级销售佣金");
					Logger.verifyEquals(new BigDecimal("1.280000"),p.getSaleCommissionMapNew().get(2),"验证2级销售佣金");
					Logger.verifyEquals(new BigDecimal("1.440000"),p.getSaleCommissionMapNew().get(3),"验证3级销售佣金");
					Logger.verifyEquals(new BigDecimal("1.600000"),p.getSaleCommissionMapNew().get(4),"验证4级销售佣金");
				}
			});

		} catch (Exception e) {
			Logger.fail(e);
		}
	}


}